package com.lingyun.ucm.client.config.springext;

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
import org.springframework.util.StringValueResolver;
/**
 * 
 * @author Terrence2013
 * 2018年1月24日 下午2:30:27
 * TODO
 */
public class ZookeeperPropertiesPlaceholderConfigurer extends ZookeeperPlaceholderConfigurerSupport {

	@Override
	protected void processProperties(
			ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
			throws BeansException {
		StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props);
		this.doProcessProperties(beanFactoryToProcess, valueResolver);
	}
	
	private class PlaceholderResolvingStringValueResolver implements StringValueResolver {
		private final PropertyPlaceholderHelper helper;
		private final PlaceholderResolver resolver;
		public PlaceholderResolvingStringValueResolver(Properties props) {
			this.helper = new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
			this.resolver = new PropertyPlaceholderConfigurerResolver(props);
		}
		public String resolveStringValue(String strVal) throws BeansException {
			String value = this.helper.replacePlaceholders(strVal, this.resolver);
			return (value.equals(nullValue) ? null : value);
		}
	}
	
	private class PropertyPlaceholderConfigurerResolver implements PlaceholderResolver {
		private final Properties props;
		private PropertyPlaceholderConfigurerResolver(Properties props) {
			this.props = props;
		}
		public String resolvePlaceholder(String placeholderName) {
			return props.getProperty(placeholderName);
		}
	}

}
